<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="limits" uri="/WEB-INF/tld/limits.tld"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>行业地市用户时间段统计</title>
<link rel="stylesheet" type="text/css"	href="../static/js/themes/defalut.css">
<link rel="stylesheet" type="text/css"	href="../static/js/themes/public.css">
<link rel="stylesheet" type="text/css"	href="../static/js/themes/ddspstyle.css">
<link rel="stylesheet" type="text/css"	href="../static/js/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"	href="../static/js/themes/icon.css">
<link rel="stylesheet" type="text/css"	href="../static/js/WdatePicker/skin/WdatePicker.css">
<script type="text/javascript" src="../static/js/jquery.min.js"></script>
<script type="text/javascript" src="../static/js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../static/js/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="../static/js/index.js"></script>
<script type="text/javascript" src="../static/js/WdatePicker/WdatePicker.js"></script>

<script type="text/javascript">
	var queryParams;
	
	function EasyuiDatagridCityAndUsers() {
		$("#easyui-datagrid").datagrid({
			url: 'selectTemplateStatisticsList.do',
			nowrap: true,
			striped: true,
			border: false,
			fit: true,
			rownumbers:true,
			queryParams: getGueryParams(),
			columns: [[
				{field:'f_cc_name',title:'客户账号',align:'center',width:150},
				{field:'f_po_address',title:'地市',align:'center',width:150},
				{field:'f_po_industry',title:'行业单位',align:'center',width:150},
				{field:'f_po_name',title:'单位名称',align:'center',width:300},
				{field:'f_po_model_name',title:'模板名称',align:'center',width:150},
				{field:'f_cc_hy',title:'是否活跃',align:'center',width:150}
			]],
			singleSelect:false,
			pagination:true,
			pageList:[10,20,30,40,50],
			pageSize:10,
			toolbar: '#toolbar',
			
			onExpandRow: function(index,row){
			
			},
			onSelect: function(rowIndex,rowData) {
				
			},
			onUnselect: function(rowIndex,rowData) {
				
			},
			onCheckAll: function(rowIndex,rowData) {
				
			},
			onUncheckAll: function(rowIndex,rowData) {
			
			},
			onLoadSuccess: function(data) {
				if(data.total==-1) {
					$.messager.defaults={ok:"确定",cancel:"取消"};
					$.messager.confirm('提示', '用户尚未登录,请登录', function(r){
					if (r){
					    window.parent.location.href='/login.do';
					    return false;
					}});
				}else{
                    updateExportBtn(data);
                }
			}
		});
	}
	

	
	function getGueryParamsStatisticsCount(){
		var replaceMap = {};
		var startDate = $("#startDate").val();
		var endDate = $("#endDate").val();
		if(typeof startDate == "undefined" || startDate == null || startDate == ""){
			var startDate = getLastMonth();
			replaceMap['lastMonth']=startDate;
	    }else{
			replaceMap['lastMonth']=startDate;
		}
		if(typeof endDate == "undefined" || endDate == null || endDate == ""){
			var endDate = getLastMonth();
			replaceMap['currentMonth']=endDate;
		}else{
			replaceMap['currentMonth']=endDate;
		}

		if($("#activeState").val()==1){
			return {
				dynamicSqlCountKey:"local_active_users_statistics_period_count",
				dynamicSqlListKey:"",
				page:0,
				rows:10,
				replaceParam:replaceMap
			};
		}else if($("#activeState").val()==2){
			return {
				dynamicSqlCountKey:"local_active_users_statistics_period_active_count",
				dynamicSqlListKey:"",
				page:0,
				rows:10,
				replaceParam:replaceMap
			};
		}else if($("#activeState").val()==3){
			return {
				dynamicSqlCountKey:"local_active_users_statistics_period_silence_count",
				dynamicSqlListKey:"",
				page:0,
				rows:10,
				replaceParam:replaceMap
			};
		}

	}
	
	function getGueryParams(){

		var replaceMap = {};
		var startDate = $("#startDate").val();
		var endDate = $("#endDate").val();
		if(typeof startDate == "undefined" || startDate == null || startDate == ""){
			var startDate = getLastMonth();
			replaceMap['lastMonth']=startDate;
		}else{
			replaceMap['lastMonth']=startDate;
		}
		if(typeof endDate == "undefined" || endDate == null || endDate == ""){
			var endDate = getLastMonth();
			replaceMap['currentMonth']=endDate;
		}else{
			replaceMap['currentMonth']=endDate;
		}

		if($("#activeState").val()==1){
			return {
				dynamicSqlCountKey:"local_active_users_statistics_period_count",
				dynamicSqlListKey:"local_active_users_statistics_period_list",
				replaceParam:replaceMap
			};
		}else if($("#activeState").val()==2){
			return {
				dynamicSqlCountKey:"local_active_users_statistics_period_active_count",
				dynamicSqlListKey:"local_active_users_statistics_period_active_list",
				replaceParam:replaceMap
			};
		}else if($("#activeState").val()==3){
			return {
				dynamicSqlCountKey:"local_active_users_statistics_period_silence_count",
				dynamicSqlListKey:"local_active_users_statistics_period_silence_list",
				replaceParam:replaceMap
			};
		}

	}
	
	$(function(){
		
		var lastMonth = getLastMonth();
		$("#startDate").val(lastMonth);
		$("#endDate").val(lastMonth);

		updateActiveState()

		gotoSearch = function(){
			new EasyuiDatagridCityAndUsers();
		};
	});
	
	function load(){
		$(".datagrid-row-expander datagrid-row-expand").attr("display","none"); 
	}

	function updateActiveState(){
		$("#selectTemplateStatisticsExcelListIds").html("正在加载导出按钮.....");
		<limits:uiAuth url="selectTemplateStatisticsExcelList.do">
		$.ajax({
			url:"selectTemplateStatisticsList.do",
			data:getGueryParamsStatisticsCount(),
			dataType: "json",
			type: "POST",
			success:function(data) {
                updateExportBtn(data);
			}
		});
		</limits:uiAuth>
	}
	function updateExportBtn(data){
        var str ="";
        if(data.total>50000){
            var pageCount = Math.ceil(data.total/50000);
            for(var i=0;i<pageCount;i++){
                if(i==pageCount-1){
                    var startData = i*50000;
                    var endData = data.total-i*50000;
                    str += '<a startData="'+startData+'" endData="'+endData+'" class="easyui-linkbutton l-btn l-btn-plain" href="javascript:void(0);" plain="true" onclick="javascript:exportExcelStatisticsList(this);" ><span class="l-btn-left"><span class="l-btn-text icon-save l-btn-icon-left">剩余的数据</span></span></a>';
                }else{
                    var startData = i*50000;
                    var endData = 50000;
                    var exportStartData = "";
                    if(i==0){
                        exportStartData = "1"
                    }else{
                        exportStartData = (i*5)+"万"
                    }
                    var exportEndData = ((i+1)*5)+"万";
                    str += '<a startData="'+startData+'" endData="'+endData+'" class="easyui-linkbutton l-btn l-btn-plain" href="javascript:void(0);" plain="true" onclick="javascript:exportExcelStatisticsList(this);" ><span class="l-btn-left"><span class="l-btn-text icon-save l-btn-icon-left">'+exportStartData+'到'+exportEndData+'的数据</span></span></a>';
                }

            }
            $("#selectTemplateStatisticsExcelListIds").html(str);
        }else{
            var startData = 0;
            var endData = data.total;
            str += '<a startData="'+startData+'" endData="'+endData+'" class="easyui-linkbutton l-btn l-btn-plain" href="javascript:void(0);" plain="true" onclick="javascript:exportExcelStatisticsList(this);" ><span class="l-btn-left"><span class="l-btn-text icon-save l-btn-icon-left">导出数据</span></span></a>';
            $("#selectTemplateStatisticsExcelListIds").html(str);
        }
    }
	function exportExcelStatisticsList(e){
		
		var ALabel = $(e);
		var firstResult = ALabel.attr("startData");
		var maxResults = ALabel.attr("endData");

		var dynamicSqlCountKey="";
		var dynamicSqlListKey="";
		if($("#activeState").val()==1){
			dynamicSqlCountKey="local_active_users_statistics_period_count";
			dynamicSqlListKey="local_active_users_statistics_period_excel_list";
		}else if($("#activeState").val()==2){
			dynamicSqlCountKey="local_active_users_statistics_period_active_count";
			dynamicSqlListKey="local_active_users_statistics_period_active_excel_list";
		}else if($("#activeState").val()==3){
			dynamicSqlCountKey="local_active_users_statistics_period_silence_count";
			dynamicSqlListKey="local_active_users_statistics_period_silence_excel_list";
		}

		var replaceMap = {};
		var startDate = $("#startDate").val();
		var endDate = $("#endDate").val();
		if(typeof startDate == "undefined" || startDate == null || startDate == ""){
			var startDate = getLastMonth();
			replaceMap['lastMonth']=startDate;
		}else{
			replaceMap['lastMonth']=startDate;
		}
		if(typeof endDate == "undefined" || endDate == null || endDate == ""){
			var endDate = getLastMonth();
			replaceMap['currentMonth']=endDate;
		}else{
			replaceMap['currentMonth']=endDate;
		}
		replaceMap['firstResult']=firstResult;
		replaceMap['maxResults']=maxResults;
		
		var titleMap = {};
		titleMap['f_cc_name']=encodeURIComponent("客户账号");
		titleMap['f_po_address']=encodeURIComponent("地市");
		titleMap['f_po_industry']=encodeURIComponent("行业单位");
		titleMap['f_po_name']=encodeURIComponent("单位名称");
		titleMap['f_po_model_name']=encodeURIComponent("模板名称");
		titleMap['f_cc_hy']=encodeURIComponent("是否活跃");
		
		var mapJson=JSON.stringify(replaceMap);
		var titleJson=JSON.stringify(titleMap);
		titleJson=encodeURIComponent(titleJson);
		mapJson=encodeURIComponent(mapJson);
		var url="selectTemplateStatisticsExcelList.do?dynamicSqlCountKey="+dynamicSqlCountKey+"&dynamicSqlListKey="+dynamicSqlListKey+"&replaceJson="+mapJson+"&titleJson="+titleJson;
        window.open(url);
		
	}

	function getLastMonth(){//获取上个月日期
	    var date = new Date; 
	    var year = date.getFullYear();
	    var month = date.getMonth();
	    if(month == 0){
	         year = year -1;
	         month = 12; 
	    }
		month =(month<10 ? "0"+month:month);
	    return year+"-"+month+"-01";
	}

</script>
</head>
<body onload="load();">
	<table id="easyui-datagrid" title="行业模板统计"></table>
	<div id="toolbar" class="easyui_datagrid_toolbar" style="">
		<div class="easyuiSearch">
			<form onsubmit="return false;" method="post">
				<table cellspacing="0" cellpadding="0" width="100%">
					
					<tr>
						<td align="left" width="7%">
							查询日期：
						</td>
						<td align="left" width="15%">
							<input id="startDate" name="startDate" type="text" onClick="WdatePicker()" style="width: 150px;" value="" class="yl_inp yw_name" />
						</td>
						<td align="left" width="5%">
							&nbsp至
						</td>
						<td align="left" >
							<input id="endDate" name="endDate" type="text" onClick="WdatePicker()" style="width: 150px;" value="" class="yl_inp yw_name" />
						</td>

						<td align="left" width="7%">
							活跃状态：
						</td>
						<td align="left" width="15%">
							<select name="activeState" id="activeState" style="width: 120px;" onchange="updateActiveState()">
								<option value="1" selected="selected">全部</option>
								<option value="2" >活跃状态</option>
								<option value="3" >不活跃状态</option>
							</select>
						</td>

						<td align="left" width="6%">
							<limits:uiAuth url="selectTemplateStatisticsList.do">
								<a href="javascript:void(0);" class="easyui-linkbutton"
									data-options="iconCls:'icon-search', plain:true"
									onclick="gotoSearch($('#easyui-datagrid'))">查询</a>
							</limits:uiAuth>
						</td>
						
						<td align="left" id="selectTemplateStatisticsExcelListIds">
							<limits:uiAuth url="selectTemplateStatisticsExcelList.do">
								正在加载导出按钮.....
							</limits:uiAuth>
						</td>
						
					</tr>
					
				</table>
			</form>
		</div>
	</div>
	
</body>
</html>